/*
 * =====================================================================================
 *
 *       Filename:  cl_string_tokenizer.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  12/06/2010 07:16:28 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include "cl_string_tokenizer.hxx"

namespace neo
{
using namespace std;

CLStringTokenizer::CLStringTokenizer(char delimiter)
	: m_delimiter(delimiter)
{
}

CLStringTokenizer::~CLStringTokenizer()
{
}

void CLStringTokenizer::tokenize(const string& entry, CLStringTokenizer::StringVector& vs) const  
{
	const char* lpos = entry.c_str();
	const char* rpos = lpos;
	const char* begin = lpos;

	for(; *rpos != '\0';)
	{
    while((*rpos != '\0') && (*rpos != m_delimiter)) 
		{
			++rpos;
		}

		if(*rpos != '\0')
		{
  		if(((lpos == rpos-1) && (*lpos == m_delimiter)) || (lpos == rpos))
	  	{
	  		vs.push_back("");
	  	}
		  else
		  {
			  vs.push_back(string(lpos, rpos - lpos));
		  }
			++rpos;
			lpos = rpos;
		}
		else 
		{
			if(*(rpos-1) == m_delimiter)
   		{
	      vs.push_back("");
	   	}
			else if(lpos != begin)
			{
			  vs.push_back(string(lpos, rpos - lpos));
			}
		}
	}

	if((rpos != begin) && (*(rpos-1) == m_delimiter))
	{
    vs.push_back("");
	}
}

} // neo
